package com.woniuxueyuan.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Map;

public class UserContext {
    public static Integer getUserId() throws ParseException {
//        RequestContextHolder:tomcat:tomacat封装请求上下文
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();


        String headerJWT = request.getHeader("authorization");
        Map userInfo = JWTUtil.getJWTUserInfo(headerJWT);
        return((Long)userInfo.get("id")).intValue();
    }
}
